package com.arvin.faceDetect.utils

import android.content.Context
import android.content.SharedPreferences
import android.util.Size
import com.arvin.faceDetect.model.CameraSettings
import com.google.mlkit.vision.face.FaceDetectorOptions

object Settings {
    private const val PREF_NAME = "face_detect_settings"
    
    // 识别设置
    private const val KEY_RECOGNITION_THRESHOLD = "recognition_threshold"
    private const val KEY_MIN_CONFIRMATIONS = "min_confirmations"
    private const val KEY_MIN_FACE_SIZE = "min_face_size"
    private const val KEY_RECOGNITION_INTERVAL = "recognition_interval"
    private const val KEY_RECOGNITION_PERFORMANCE_MODE = "recognition_performance_mode"
    private const val KEY_RECOGNITION_MIN_FACE_SIZE = "recognition_min_face_size"
    private const val KEY_RECOGNITION_ENABLE_TRACKING = "recognition_enable_tracking"
    const val KEY_SHOW_FACE_RECT = "show_face_rect"
    const val KEY_SHOW_EYE_RECT = "show_eye_rect"
    
    // 采集设置
    const val KEY_COLLECTION_REQUIRED_FRAMES = "collection_required_frames"
    const val KEY_COLLECTION_THRESHOLD = "collection_threshold"
    const val KEY_COLLECTION_INTERVAL = "collection_interval"
    private const val KEY_COLLECTION_PERFORMANCE_MODE = "collection_performance_mode"
    private const val KEY_COLLECTION_MIN_FACE_SIZE = "collection_min_face_size"
    
    // 相机设置
    private const val KEY_CAMERA_RESOLUTION = "camera_resolution"
    private const val KEY_AUTO_FOCUS = "auto_focus"
    private const val KEY_FLASH = "flash"
    private const val KEY_MAX_FACE_SIZE = "max_face_size"
    private const val KEY_CAMERA_FACING = "camera_facing"
    
    // 系统设置
    private const val KEY_LOG_RETENTION_DAYS = "log_retention_days"
    private const val KEY_DEBUG_MODE = "debug_mode"
    private const val KEY_SHOW_FLOATING_BUTTON = "show_floating_button"

    private fun getPrefs(context: Context): SharedPreferences {
        return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
    }

    // 采集设置相关方法
    fun getCollectionRequiredFrames(context: Context): Int {
        return getPrefs(context).getInt(KEY_COLLECTION_REQUIRED_FRAMES, 3)
    }

    fun setCollectionRequiredFrames(context: Context, frames: Int) {
        getPrefs(context).edit().putInt(KEY_COLLECTION_REQUIRED_FRAMES, frames).apply()
    }

    fun getCollectionThreshold(context: Context): Float {
        return getPrefs(context).getFloat(KEY_COLLECTION_THRESHOLD, 0.85f)
    }

    fun setCollectionThreshold(context: Context, threshold: Float) {
        getPrefs(context).edit().putFloat(KEY_COLLECTION_THRESHOLD, threshold).apply()
    }

    fun getCollectionInterval(context: Context): Int {
        return getPrefs(context).getInt(KEY_COLLECTION_INTERVAL, 5)
    }

    fun setCollectionInterval(context: Context, interval: Int) {
        getPrefs(context).edit().putInt(KEY_COLLECTION_INTERVAL, interval).apply()
    }

    // 新增采集性能模式设置
    fun getCollectionPerformanceMode(context: Context): Int {
        return getPrefs(context).getInt(
            KEY_COLLECTION_PERFORMANCE_MODE,
            FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE
        )
    }

    fun setCollectionPerformanceMode(context: Context, mode: Int) {
        getPrefs(context).edit().putInt(KEY_COLLECTION_PERFORMANCE_MODE, mode).apply()
    }

    // 新增采集最小人脸尺寸设置
    fun getCollectionMinFaceSize(context: Context): Float {
        return getPrefs(context).getFloat(KEY_COLLECTION_MIN_FACE_SIZE, 0.3f)
    }

    fun setCollectionMinFaceSize(context: Context, size: Float) {
        getPrefs(context).edit().putFloat(KEY_COLLECTION_MIN_FACE_SIZE, size).apply()
    }

    // 新增识别性能模式设置
    fun getRecognitionPerformanceMode(context: Context): Int {
        return getPrefs(context).getInt(
            KEY_RECOGNITION_PERFORMANCE_MODE,
            FaceDetectorOptions.PERFORMANCE_MODE_FAST
        )
    }

    fun setRecognitionPerformanceMode(context: Context, mode: Int) {
        getPrefs(context).edit().putInt(KEY_RECOGNITION_PERFORMANCE_MODE, mode).apply()
    }

    // 新增识别最小人脸尺寸设置
    fun getRecognitionMinFaceSize(context: Context): Float {
        return getPrefs(context).getFloat(KEY_RECOGNITION_MIN_FACE_SIZE, 0.2f)
    }

    fun setRecognitionMinFaceSize(context: Context, size: Float) {
        getPrefs(context).edit().putFloat(KEY_RECOGNITION_MIN_FACE_SIZE, size).apply()
    }

    // 新增识别人脸跟踪设置
    fun isRecognitionTrackingEnabled(context: Context): Boolean {
        return getPrefs(context).getBoolean(KEY_RECOGNITION_ENABLE_TRACKING, true)
    }

    fun setRecognitionTrackingEnabled(context: Context, enabled: Boolean) {
        getPrefs(context).edit().putBoolean(KEY_RECOGNITION_ENABLE_TRACKING, enabled).apply()
    }

    // 识别设置
    fun getRecognitionThreshold(context: Context): Float {
        return getPrefs(context).getFloat(KEY_RECOGNITION_THRESHOLD, 0.65f)
    }

    fun setRecognitionThreshold(context: Context, threshold: Float) {
        getPrefs(context).edit().putFloat(KEY_RECOGNITION_THRESHOLD, threshold).apply()
    }

    fun getMinConfirmations(context: Context): Int {
        return getPrefs(context).getInt(KEY_MIN_CONFIRMATIONS, 2)
    }

    fun setMinConfirmations(context: Context, confirmations: Int) {
        getPrefs(context).edit().putInt(KEY_MIN_CONFIRMATIONS, confirmations).apply()
    }

    fun getRecognitionInterval(context: Context): Int {
        return getPrefs(context).getInt(KEY_RECOGNITION_INTERVAL, 30)
    }

    fun setRecognitionInterval(context: Context, interval: Int) {
        getPrefs(context).edit().putInt(KEY_RECOGNITION_INTERVAL, interval).apply()
    }

    fun isShowFaceRect(context: Context): Boolean {
        return getPrefs(context).getBoolean(KEY_SHOW_FACE_RECT, true)
    }

    fun setShowFaceRect(context: Context, show: Boolean) {
        getPrefs(context).edit().putBoolean(KEY_SHOW_FACE_RECT, show).apply()
    }

    fun isShowEyeRect(context: Context): Boolean {
        return getPrefs(context).getBoolean(KEY_SHOW_EYE_RECT, true)
    }

    fun setShowEyeRect(context: Context, show: Boolean) {
        getPrefs(context).edit().putBoolean(KEY_SHOW_EYE_RECT, show).apply()
    }

    // 相机设置
    enum class CameraResolution {
        HIGH, MEDIUM, LOW
    }

    fun getCameraFacing(context: Context): Int {
        return getPrefs(context).getInt(KEY_CAMERA_FACING, CameraSettings.CAMERA_FACING_FRONT)
    }

    fun setCameraFacing(context: Context, facing: Int) {
        getPrefs(context).edit().putInt(KEY_CAMERA_FACING, facing).apply()
    }

    fun getCameraResolution(context: Context): CameraResolution {
        val value = getPrefs(context).getString(KEY_CAMERA_RESOLUTION, CameraResolution.MEDIUM.name)
        return CameraResolution.valueOf(value ?: CameraResolution.MEDIUM.name)
    }

    fun setCameraResolution(context: Context, resolution: CameraResolution) {
        getPrefs(context).edit().putString(KEY_CAMERA_RESOLUTION, resolution.name).apply()
    }

    fun getCameraResolutionSize(context: Context): Size {
        return when (getCameraResolution(context)) {
            CameraResolution.HIGH -> Size(1920, 1080)
            CameraResolution.MEDIUM -> Size(1280, 720)
            CameraResolution.LOW -> Size(640, 480)
        }
    }

    fun isAutoFocusEnabled(context: Context): Boolean {
        return getPrefs(context).getBoolean(KEY_AUTO_FOCUS, true)
    }

    fun setAutoFocusEnabled(context: Context, enabled: Boolean) {
        getPrefs(context).edit().putBoolean(KEY_AUTO_FOCUS, enabled).apply()
    }

    fun isFlashEnabled(context: Context): Boolean {
        return getPrefs(context).getBoolean(KEY_FLASH, false)
    }

    fun setFlashEnabled(context: Context, enabled: Boolean) {
        getPrefs(context).edit().putBoolean(KEY_FLASH, enabled).apply()
    }

    // 系统设置
    fun getLogRetentionDays(context: Context): Int {
        return getPrefs(context).getInt(KEY_LOG_RETENTION_DAYS, 7)
    }

    fun isDebugModeEnabled(context: Context): Boolean {
        return getPrefs(context).getBoolean(KEY_DEBUG_MODE, false)
    }

    fun getMinFaceSize(context: Context): Float {
        return getPrefs(context).getFloat(KEY_MIN_FACE_SIZE, 0.2f)
    }

    fun setMinFaceSize(context: Context, size: Float) {
        getPrefs(context).edit().putFloat(KEY_MIN_FACE_SIZE, size).apply()
    }

    fun getMaxFaceSize(context: Context): Float {
        return getPrefs(context).getFloat(KEY_MAX_FACE_SIZE, 0.8f)
    }

    fun setMaxFaceSize(context: Context, size: Float) {
        getPrefs(context).edit().putFloat(KEY_MAX_FACE_SIZE, size).apply()
    }

    fun isShowFloatingButton(context: Context): Boolean {
        return getPrefs(context).getBoolean(KEY_SHOW_FLOATING_BUTTON, false)
    }

    fun setShowFloatingButton(context: Context, enabled: Boolean) {
        getPrefs(context).edit().putBoolean(KEY_SHOW_FLOATING_BUTTON, enabled).apply()
    }
} 